草庐IT

C++ 空类或 typedef

全部标签

c++ - 为什么当一个模板类继承另一个模板类时,需要重新指定typedefs和函数调用限定?

当一个模板类继承另一个模板类时,基类中的typedef必须重新定义(即不会自动继承),基类中的函数调用需要限定。这是为什么?这不是已经很明确了吗?因此,如果我有20个模板类,都定义了相同的typedef,我将无法引入包含这些定义的基类并从中继承,因为无论如何我都必须在每个类中重新定义typedef,这会破坏目的。这使得源代码变得不必要地冗长。我可以看到这已在question中讨论过,但我不明白评论TheC++namelookuprulesspecifythatanameisonlysearchedinatemplatedbaseclassesifitdependsonatemplate

c++ - 为什么 typedef 模板是非法的?

从实际的角度来看,我理解typedef和test都有些“多余”,如果我们要编译以下代码,则需要将其删除:templatetypedefstructtagTest{inta;}test;但是,我认为typedef声明集是声明集的子集。他们只是碰巧有那个特定的decl-specifier。这是我的合理化typedefstructtagTest{inta;}test;引入标识符test并声明结构tagTest。如果该解释是正确的,那么标准中的以下段落应该允许templatetypedef(尽管不是关键字using给出的含义)>).Thedeclarationinatemplate-decla

c++ - typedef 和 using 会导致模板实例化吗?

假设我有一个这样定义的模板类templateclassTemp{//irrelevant};我可以隐式或显式实例化它:Tempti;templateclassTemp;通过显式实例化,我的程序应该包含一个实例,即使我以后不使用它(假设它没有被编译器优化省略)。我的问题是,下面的语句会导致类的实例化吗?typedefTempTShort;usingTFloat=Temp;//C++11 最佳答案 没有。Implicitinstantiation仅在需要完全定义的类型时才会发生;而类型别名则不必。Whencodereferstoatem

c++ - 是否可以在没有 typedef 的情况下声明返回数组引用的转换函数?

这是一个返回数组引用的转换函数:structS{typedefintint_array_20[20];operatorint_array_20&();};没有typedef是否可以做同样的事情?我试过的:structS{operatorint(&())[10];};但是clang提示:error:C++requiresatypespecifierforalldeclarationsoperatorint(&())[10];~^error:conversionfunctioncannothaveanyparametersoperatorint(&())[10];^error:mustus

C++ typedef 修复模板参数

从c++11开始,我们可以这样做:templateclassC{};templateusingD=C;所以D是C且B=int。有没有办法通过c++03中的typedef做到这一点?这不起作用:templatetypedefCD; 最佳答案 不可能这么简单。在C++03中唯一可以作为模板的是类和函数。关于类的好处是它们本身可以包含typedef作为成员(member)。templatestructD{typedefCtype;};所以现在D::type代表C.这就是模板元编程中所谓的元函数。它很好,因为您可以在C++03中实现它。虽然

c++ - "using"能否在所有情况下完全替换 "typedef"?

我知道using可以做typedef做不到的事情。我只是想知道使用是否可以在所有情况下完全替代typedef? 最佳答案 是的,引用自draftStandard(大胆强调我的)7.1.3typedef说明符[dcl.typedef]2Atypedef-namecanalsobeintroducedbyanalias-declaration.Theidentifierfollowingtheusingkeywordbecomesatypedef-nameandtheoptionalattribute-specifier-seqfollow

c++ - 为什么我不能在 Arduino 中传递 typedef 或 enum?

以下草图无法在Arduino环境中编译。鉴于typedefscanbeusedwithinArduinosoftware,AutomaticPrototypeGeneration是导致失败的底层机制吗?如果是,它是什么?为什么Arduino不提供围绕C++的轻量级包装器?#definePRODUCE_WACKY_COMPILETIME_ERRORtypedefintMyMeaningfulType;#ifndefPRODUCE_WACKY_COMPILETIME_ERRORvoidmyFunc(MyMeaningfulTypemyParam);#endifvoidmyFunc(MyMe

c++ - std::tuple 中的空类

Thesizeofanyobjectormembersubobjectisrequiredtobeatleast1evenifthetypeisanemptyclasstype[...],inordertobeabletoguaranteethattheaddressesofdistinctobjectsofthesametypearealwaysdistinct.cppreferencequote这个我就知道了。我刚发现的是一些库类型,如std::tuple不使用任何大小来包含空类。这是真的?如果是,那怎么样?编辑:在阅读了@bolov对他的回答的最后注释后,我还有一个问题:因为Em

C++ BOOL (typedef int) vs bool 性能

我在某处读到,使用BOOL(typedefint)比使用标准的c++类型bool更好,因为BOOL的大小是4个字节(即4的倍数),并且它将变量的对齐操作保存到寄存器或其他东西中线...这有什么道理吗?我想即使您使用bool(1字节),编译器也会填充堆栈帧以保持4的倍数对齐?我绝不是对齐、寄存器等基础工作方面的专家,所以如果我完全错了,我提前道歉。希望指正。:)干杯! 最佳答案 首先,sizeof(bool)不一定是1。是implementation-defined,让编译器编写者可以自由选择适合目标平台的大小。此外,sizeof(i

c++ - typedef 的名称查找在 GNU 编译器中有问题吗?

下面的代码#includetypedefdoubleA;//aglobaltypedeftemplatestructB//atemplateclass...{Ai{22.2};//globaltypedefisinscopetypedefintA;//nowalocaltypedefwiththesamenameisintroducedAb{24};//nowthelocaltypedefisinscopeZc{36};//asimplememberofthetemplatetype};templatestructC:B//atemplatestructinheritingB{Aa;/